package com.kbbywyclient.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Description:
 * Created by FHW
 * on 2017/4/14
 */

public class DateUtils {

    private static SimpleDateFormat sf = null;

    /*获取系统时间 格式为："yyyy/MM/dd "*/
    public static String getCurrentDate() {
        Date d = new Date();
        sf = new SimpleDateFormat("yyyy年MM月dd日");
        return sf.format(d);
    }

    /*时间戳转换成字符窜*/
    public static String getDateToString(long time) {
        time = time * 1000;
        Date d = new Date(time);
        sf = new SimpleDateFormat("yyyy-MM-dd");
        return sf.format(d);
    }

    /*将字符串转为时间戳*/
    public static long getStringToDate(String time) {
        sf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        try {
            date = sf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime() / 1000;
    }

    public static long getStrMonthDate(int days) {

        return days * 24 * 60 * 60 * 1000;
    }

    /**
     * @param distance 和今天的天数差
     * @param format   返回日期格式
     * @return 根据和今天的天数差获取任意时间的日期，例：参数是1获取昨天的日期，为0获取今天的日期，是-1获取明天的日期
     */
    public static String getAnyData(int distance, String format) {
        try {
            DateFormat df = new SimpleDateFormat(format);
            String today = df.format(new Date());
            Date date = df.parse(today);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - distance);
            return df.format(calendar.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }

}
